package com.situ.blog.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

// /page/blog/list
// /page/blog/update
// /page/user/list
// /page/login
@WebServlet("/page/*")
public class PageServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        System.out.println("requestURI: " + requestURI);
        //requestURI: /page/blog/list
        //requestURI: /page/blog/update
        //requestURI: /page/user/update
        String[] paths = requestURI.split("/");
        System.out.println(Arrays.toString(paths));
        //["", page, blog, list]
        //["", page, login]
        if (paths.length == 4) {
            req.getRequestDispatcher("/WEB-INF/page/" + paths[2] + "_" + paths[3] + ".jsp").forward(req, resp);
        } else if (paths.length == 3) {
            req.getRequestDispatcher("/WEB-INF/page/" + paths[2]  + ".jsp").forward(req, resp);
        }
    }
}
